package com.authorwjf;

import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

public class Player extends Activity {
	private static String mFileName = null;
	boolean mStartPlaying = true;

	private Button mPlayButton = null;
	private MediaPlayer mPlayer = null;
	private SeekBar progress = null;
	private int durata=0;
	private TextView a = null;		
@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_player);
		Intent intent = getIntent();
		
		mFileName = intent.getStringExtra(Main.EXTRA_MESSAGE);
		mPlayButton = (Button) findViewById(R.id.button5);
		progress= (SeekBar) findViewById(R.id.seekBar1);
		a=(TextView) findViewById(R.id.textView1);
		
		mPlayButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {


				onPlay(mStartPlaying);
				if (mStartPlaying) {
					mPlayButton.setText("Stop playing");
				System.out.println(durata);
				a.setText(Integer.toString(durata));
				progress.setMax(durata);
				

					
				} else {
					mPlayButton.setText("Start playing");
				}
				mStartPlaying = !mStartPlaying;
			}
		});

	}
	private void onPlay(boolean start) {
		if (start) {
			startPlaying();
		} else {
			stopPlaying();
		}
	}
	private void startPlaying() {
		mPlayer = new MediaPlayer();
		try {
			mPlayer.setDataSource(mFileName);
			mPlayer.prepare();
			mPlayer.start();
			durata=mPlayer.getDuration();
		} catch (IOException e) {
		}
	}
	private void stopPlaying() {
		mPlayer.release();
		mPlayer = null;
	}
	protected void onPause() {
		super.onPause();
		if (mPlayer != null) {
			mPlayer.release();
			mPlayer = null;
		}
	}


}
